
#ifndef _COMMON_H_
#define _COMMON_H_

#include "sys_common.h"

#include <math.h>

/* libxml2 include */
#include <libxml/parser.h>
#include <libxml/tree.h>
#include <libxml/xpath.h>
/* openssl support */
#include <openssl/ssl.h>
#include <openssl/x509.h>
#include <openssl/err.h>
#include <openssl/rand.h>
#include <openssl/md5.h>
#include <openssl/des.h>
#include <openssl/hmac.h>
/* pthread support */
#include <pthread.h>
/* Glib2.0 */
#include <glib.h>

#include "list.h"
#include "array.h"
#include "header.h"
#include "log.h"
#include "md5.h"
#include "conf.h"
#include "sock_event.h"
#include "globals.h"

typedef void WPF(int, int, int, void *arg);
typedef void PF(int, short, void *);

/* default value of conf */

#define DEFAULE_CACHE_DOWNLOAD_WAITING 1
#define DEFAULT_PORT 80
#define DEFAULT_BIND_ADDR4 "0.0.0.0"
#define DEFAULT_DEBUG_MODE 0
#define DEFAULT_DEBUG_LEVEL 1
#define DEFAULT_WORKER_NUMBER 4
#define DEFAULT_MAX_SIZE_IN_MEM (1<<20)
#define DEFAULT_MAX_SIZE_IN_SHM (1<<20)
#define DEFAULT_OBJECTS 12582910LL
#define DEFAULT_MAX_OBJECT_SIZE_IN_MEM 8192LL
#define DEFAULT_MAXFDS 204800

#define DEFAULT_CLIENT_LIFE (24*60*60)
#define DEFAULT_SERVER_LIFE (24*60*60)
#define DEFAULT_REQUEST_TIMEOUT (60)
#define DEFAULT_REPLY_TIMEOUT (60)
#define DEFAULT_CONNECT_TIMEOUT (60)

#define DIGEST_LENGTH 16
#define MAX_HEADER 4096
#define MAX_URL 4096
#define PAGE_SIZE 4096
#define DEFAULT_MAP_SIZE (4096 * 4)

/* compute array number */
#define W_SPACE " \r\n\t"
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
#define NORMAL_SIGNALS {SIGUSR1, SIGUSR2, SIGPIPE, SIGTERM, SIGHUP, SIGINT}
#define COREDUMP_SIGNALS {SIGABRT, SIGFPE, SIGILL, SIGQUIT, SIGSEGV, \
	SIGTRAP, SIGSYS, SIGBUS, SIGXCPU, SIGXFSZ}
#define XMIN(x,y) ((x)<(y)? (x) : (y))
#define XMAX(a,b) ((a)>(b)? (a) : (b))
#define current_time_get(tv) gettimeofday(tv, NULL)
#define timeval_compare(res, t1, t2) \
			{ \
					if(t1.tv_sec > t2.tv_sec) res = 1; \
					else if(t1.tv_sec < t2.tv_sec) res = -1; \
					else res = (t1.tv_usec >= t2.tv_usec ? 1 : -1); \
			}
#define timeval_is_zero(res, t) \
			{ \
					if(t.tv_sec == 0 && t.tv_usec == 0)res=1; \
					else res=0; \
			}
#define safe_free(x) if(x){free(x);x=NULL;}
#define g_string_free_safe(x, y) if(x){g_string_free(x,y);x=NULL;}
#endif
